export function getSlides(props) {
    // return () => {
    return async(dispatch) => {
        // 获取所有轮播图
        // 把token放到请求头里面去
        const response = await fetch('/slides', {
            method: 'GET',
            headers: new Headers({
                'Authorization': localStorage.getItem('token')
            })
        })
        const resData = await response.json()
        if(resData.code === 1) {
            dispatch({
                type: 'SAVE_SLIDES',
                data: resData.list
            })
        } else {
            if(Number(resData.code) === 1001) {
                props.history.push('/login')
            } else {
                alert(resData.msg)
            }
        }
    }
}
export function deleteSlide(record, props) {
    return (dispatch) => {
        console.log(123, props)
        fetch('/delete/slide', {
            method: 'POST',
            body: JSON.stringify({
                id: record.id
            }),
            headers: new Headers({
                'Authorization': localStorage.getItem('token'),
                'Content-Type': 'application/json; charset=utf-8'
            })
        }).then(res => res.json()).then(res => {
            if(res.code === 1) {
                dispatch(getSlides())
            } else {
                alert(res.msg)
            }
        })
    }
}

export function saveSlides(fileList) {
    return (dispatch) => {
        fetch('/save/slide', {
            method: 'POST',
            body: JSON.stringify({
                fileList
            }),
            headers: new Headers({
                'Authorization': localStorage.getItem('token'),
                'Content-Type': 'application/json; charset=utf-8'
            })
        }).then(res => res.json()).then(res => {
            if(res.code === 1) {
                dispatch(getSlides())
            } else {
                alert(res.msg)
            }
        })
    }
}
// 上传音乐
export function saveMusic(data) {
    return () => {
        fetch('/save/music', {
            method: 'POST',
            body: JSON.stringify(data),
            headers: new Headers({
                'Authorization': localStorage.getItem('token'),
                'Content-Type': 'application/json; charset=utf-8'
            })
        }).then(res => res.json()).then(res => {
            if(res.code === 1) {
                console.log('上传成功')
                // dispatch(getSlides())
            } else {
                alert(res.msg)
            }
        })
    }
}

// 获取所有音乐
export function fetchMusic() {
    return (dispatch) => {
        fetch('/music', {
            method: 'GET',
            headers: new Headers({
                'Authorization': localStorage.getItem('token'),
                'Content-Type': 'application/json; charset=utf-8'
            })
        }).then(res => res.json()).then(res => {
            if(res.code === 1) {
                console.log('上传成功')
                // dispatch(getSlides())
                dispatch({
                    type: 'SAVE_SONGS',
                    data: res.list
                })
            } else {
                alert(res.msg)
            }
        })
    }
}